package com.zlf.sb.demo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlf.sb.demo.entity.McMembers;
import com.zlf.sb.demo.mapper.McMembersMapper;
import com.zlf.sb.demo.service.ICreditRecordService;
import com.zlf.sb.demo.service.IMcMembersService;
import com.zlf.sb.demo.vo.CreditsRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
//数据同步后数据总条数对不上的问题解决
@Slf4j
//@DS("old_db")
@Service
public class McMembersServiceImpl extends ServiceImpl<McMembersMapper, McMembers> implements IMcMembersService {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private ICreditRecordService creditRecordService;

    @Override
    public void dealData() {
        int pageSize = 20000;
        AtomicInteger index = new AtomicInteger(1);
        AtomicInteger totalIndex = new AtomicInteger(0);
        Long total2 = this.baseMapper.getTotal2();
        log.info("total2:{}",total2);
        Long pageCount2 = (total2 + pageSize - 1) / pageSize; //推荐写法
        log.info("pageCount2:{}", pageCount2);
        CountDownLatch countDownLatch = new CountDownLatch(Math.toIntExact(pageCount2));
        try (SqlSession sqlSession = sqlSessionFactory.openSession();
             Cursor<CreditsRecordVO> pageCursor = sqlSession.getMapper(McMembersMapper.class).getCursorData()) {
            List<CreditsRecordVO> creditsRecordVOS = new ArrayList<>();
            for (CreditsRecordVO creditsRecordVO : pageCursor) {
                creditsRecordVOS.add(creditsRecordVO);
                totalIndex.getAndIncrement();
                log.info("total:{}", totalIndex.get());
                if (index.getAndIncrement() == pageSize) {
                    CopyOnWriteArrayList<CreditsRecordVO> creditsRecordVO2 = new CopyOnWriteArrayList<>(creditsRecordVOS);
                    log.info("creditsRecordVO2.size:{}", creditsRecordVO2.size());
                    creditRecordService.dealData(creditsRecordVO2, countDownLatch);
                    creditsRecordVOS.clear();
                    log.info("清空list:{}", creditsRecordVOS.size());
                    index.set(0);
                }
            }
            if (CollectionUtil.isNotEmpty(creditsRecordVOS)) {
                CopyOnWriteArrayList<CreditsRecordVO> creditsRecordVO3 = new CopyOnWriteArrayList<>(creditsRecordVOS);
                log.info("creditsRecordVO3.size:{}", creditsRecordVO3.size());
                creditRecordService.dealData(creditsRecordVO3, countDownLatch);
                creditsRecordVOS.clear();
                log.info("清空list2:{}", creditsRecordVOS.size());
                index.set(0);
            }
            countDownLatch.await();
        } catch (Exception e) {
            log.error("游标查询异常:{}", e.getMessage());
        }
        log.info("total:{}", totalIndex.get());
    }

    @Override
    public Long getTotal2() {
        return baseMapper.getTotal2();
    }
}
